using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class ChuyenXeRepository
	{
		public static List<ChuyenXe> SelectAll()
		{
			try
			{
				return DataContext.Instance.ChuyenXe.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChuyenXe>();
		}

		public static ChuyenXe SelectByID(int machuyenxe)
		{
			try
			{
				return DataContext.Instance.ChuyenXe.FirstOrDefault(item => item.MaChuyenXe == machuyenxe );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(ChuyenXe obj)
		{
			try
			{
				DataContext.Instance.ChuyenXe.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int machuyenxe)
		{
			try
			{
				var delitem = DataContext.Instance.ChuyenXe.FirstOrDefault(item => item.MaChuyenXe == machuyenxe );
				DataContext.Instance.ChuyenXe.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<ChuyenXe> RetrieveByID(int machuyenxe)
		{
			try
			{
				return (from item in DataContext.Instance.ChuyenXe where  item.MaChuyenXe == machuyenxe  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChuyenXe>();
		}

		public static List<ChuyenXe> SelectByMaTuyenXe(int matuyenxe)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.ChuyenXe where item.MaTuyenXe == matuyenxe select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChuyenXe>();
		}

		public static List<ChuyenXe> SelectByMaTaiXe(int mataixe)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.ChuyenXe where item.MaTaiXe == mataixe select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChuyenXe>();
		}

		public static List<ChuyenXe> SelectByMaBSXe(string mabsxe)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.ChuyenXe where item.MaBSXe == mabsxe select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChuyenXe>();
		}

	}

}
